//
//  DKCapturePipeline.h
//  DKShortVideo
//
//  Created by Keer_LGQ on 2018/4/1.
//  Copyright © 2018年 DK. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>

typedef NS_ENUM( NSInteger, DKRecordingStatus )
{
    DKRecordingStatusIdle = 0,
    DKRecordingStatusStartingRecording,
    DKRecordingStatusRecording,
    DKRecordingStatusStoppingRecording,
}; // internal state machine


@protocol DKCapturePipelineDelegate;

@interface DKCapturePipeline : NSObject

- (instancetype)initWithDelegate:(id<DKCapturePipelineDelegate>)delegate callbackQueue:(dispatch_queue_t)queue; // delegate is weak referenced

// These methods are synchronous
- (void)startRunning;
- (void)stopRunning;

// Must be running before starting recording
// These methods are asynchronous, see the recording delegate callbacks
- (void)startRecording;
- (void)stopRecording;

- (DKRecordingStatus)getRecorderStatus;
- (NSURL *)getLastedTempVideoUrl;

/**
 *  获取视频的某一帧缩略图方法
 *
 *  @param videoURLString 视频的链接地址
 *  @param time     帧时间
 *
 *  @return 视频截图
 */
+ (UIImage *)thumbnailImageForVideo:(NSURL *)videoURLString atTime:(NSTimeInterval)time;

@property(atomic) BOOL renderingEnabled; // When set to NO the GPU will not be used after the setRenderingEnabled: call returns.

@property(atomic) AVCaptureVideoOrientation recordingOrientation; // client can set the orientation for the recorded movie

- (CGAffineTransform)transformFromVideoBufferOrientationToOrientation:(AVCaptureVideoOrientation)orientation withAutoMirroring:(BOOL)mirroring; // only valid after startRunning has been called

// Stats
@property(atomic, readonly) float videoFrameRate;
@property(atomic, readonly) CMVideoDimensions videoDimensions;

@end

@protocol DKCapturePipelineDelegate <NSObject>
@required

- (void)capturePipeline:(DKCapturePipeline *)capturePipeline didStopRunningWithError:(NSError *)error;

// Preview
- (void)capturePipeline:(DKCapturePipeline *)capturePipeline previewPixelBufferReadyForDisplay:(CVPixelBufferRef)previewPixelBuffer;
- (void)capturePipelineDidRunOutOfPreviewBuffers:(DKCapturePipeline *)capturePipeline;

// Recording
- (void)capturePipelineRecordingDidStart:(DKCapturePipeline *)capturePipeline;
- (void)capturePipeline:(DKCapturePipeline *)capturePipeline recordingDidFailWithError:(NSError *)error; // Can happen at any point after a startRecording call, for example: startRecording->didFail (without a didStart), willStop->didFail (without a didStop)
- (void)capturePipelineRecordingWillStop:(DKCapturePipeline *)capturePipeline;
- (void)capturePipelineRecordingDidStop:(DKCapturePipeline *)capturePipeline;
@end

